import 'package:cat_eye/config/color_config.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share_plus/share_plus.dart';

/*
* @Package: util
* @ClassName: util
* @Author: szj
* @CreateDate: 7/28/21 3:36 PM
*/

/*
 * TODO 顶部appBar封装
 */
Widget appBar(String title,
    {Color backgroundColor, PreferredSizeWidget bottom}) {
  print("szj主题颜色${ColorConfig.themeColor}\t $backgroundColor");
  return AppBar(
    //标题居中
    centerTitle: true,
    //阴影高度
    elevation: 0,
    title: Text(
      title,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
        color: Colors.black,
      ),
    ),
    backgroundColor: backgroundColor,
    bottom: bottom,
  );
}

/*
 * TODO  跳转页面 widget 需要跳转的页面
 * widget 要跳转的页面
 * time(ms) 跳转时间
 */
PageRouteBuilder router({@required Widget widget, time = 444}) {
  return PageRouteBuilder(
    //设置平移时间
    transitionDuration: Duration(milliseconds: time),
    pageBuilder: (BuildContext context, Animation<double> animation,
        Animation<double> secondaryAnimation) {
      //FadeTransition 渐变动画
      return FadeTransition(
        opacity: animation,
        //SlideTransition 平移动画
        child: SlideTransition(
          position: Tween(begin: Offset(1.0, 0.0), end: Offset(0.0, 0.0))
              .animate(animation),
          child: widget,
        ),
      );
    },
  );
}

/*
 * TODO 底部提醒 flutter没有自带Toast 所以就用的这个
 * action: 右侧确定按钮
 */
void snackBar(BuildContext context, String title, {SnackBarAction action}) {
  //先暂停播放【避免有延迟】
  Scaffold.of(context).removeCurrentSnackBar();

  var snackBar = SnackBar(
    //浮动躺框
    behavior: SnackBarBehavior.floating,
    backgroundColor: ColorConfig.themeColor,
    content: Text(
      title,
      style: TextStyle(
          color: ColorConfig.themeColor == Colors.white
              ? Colors.black87
              : Colors.white),
    ),
    duration: Duration(seconds: 1),
    action: action,
  );
  Scaffold.of(context).showSnackBar(snackBar);
}

/*
 *  TODO  网络图片缓存  start
 */
Widget cacheImage(String url,
    {double width,
    double height,
    fit: BoxFit.cover,
    BorderRadius borderRadius,
    BoxShape shape = BoxShape.rectangle,
    bool clearMemoryCacheWhenDispose = false}) {
  return ExtendedImage.network(
    url,
    shape: shape,
    height: height,
    width: width,
    fit: fit,
    borderRadius: borderRadius,
    //图片从 tree 中移除，清掉内存缓存，以减少内存压力
    clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
  );
}

/*
 * TODO 参考文档 [https://github.com/fluttercandies/extended_image/blob/master/README-ZH.md]
 */
ImageProvider cachedNetworkImageProvider(String url) {
  return ExtendedNetworkImageProvider(url);
}
// ========  stop ===========

/*
 * TODO 三方分享 [share_plus: ^2.1.4]
 */
void share(String title, String content) {
  Share.share('$title\n$content');
}

//TODO 横屏
void setHorizontal() {
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
}

//TODO 竖屏
void setVertical() {
// 强制竖屏
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
}

//TODO 修改顶部状态栏颜色
void statusBarColor(Color color) {
  SystemUiOverlayStyle systemUiOverlayStyle =
      SystemUiOverlayStyle(statusBarColor: color);
  SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}

//TODO 主题颜色
Color themeColor() {
  return ColorConfig.themeColor == Colors.white
      ? Colors.black
      : ColorConfig.themeColor;
}

//TODO Text封装
Widget text(
  String title, {
  int maxLine = 1, //最大行数
  TextOverflow overflow = TextOverflow.ellipsis, //省略号结尾
  FontWeight fontWeight, //粗体/斜体/删除线等
  Color fontColor, //字体颜色
  double fontSize, //字体大小
  Color backgroundColor, //字体背景颜色
}) {
  return Text(
    title,
    maxLines: maxLine,
    overflow: overflow,
    style: TextStyle(
        fontWeight: fontWeight,
        color: fontColor,
        backgroundColor: backgroundColor,
        fontSize: fontSize),
  );
}
